package com.renwei.yunlong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.EnterpriseInformationActivity;
import com.renwei.yunlong.activity.InformationActivity;
import com.renwei.yunlong.activity.SettingActivity;
import com.renwei.yunlong.activity.ShareActivity;
import com.renwei.yunlong.activity.WorkSearchActivity;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.InformationBean;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的--Fragment
 * <p>
 * Created by Administrator on 2017/8/24.
 */

public class MeFragment extends BaseFragment implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_next)
    RelativeLayout rlNext;
    @Bind(R.id.rl_enterprise)
    RelativeLayout rlEnterprise;//企业信息
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;//查询
    @Bind(R.id.rl_share)
    RelativeLayout rlShare;//分享
    @Bind(R.id.rl_setting)
    RelativeLayout rlSetting;//设置

    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.tv_nickname)
    TextView tvNickname;
    @Bind(R.id.iv_star_one)
    ImageView ivStarOne;
    @Bind(R.id.iv_star_two)
    ImageView ivStarTwo;
    @Bind(R.id.iv_star_three)
    ImageView ivStarThree;
    @Bind(R.id.iv_star_four)
    ImageView ivStarFour;
    @Bind(R.id.iv_star_fives)
    ImageView ivStarFives;
    @Bind(R.id.tv_number)
    TextView tvNumber;
    @Bind(R.id.tv_job_number)
    TextView tvJobNumber;
    @Bind(R.id.tv_work_number)
    TextView tvWorkNumber;

    private Intent intent;

    private ACache mCache;

    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_me, null);
        ButterKnife.bind(this, view);
        mCache = ACache.get(getActivity());
        return view;
    }

    @Override
    public void initData() {
        tvTitle.setText("我");
        rlNext.setOnClickListener(this);
        rlEnterprise.setOnClickListener(this);
        rlSearch.setOnClickListener(this);
        rlShare.setOnClickListener(this);
        rlSetting.setOnClickListener(this);


        LoginBean ownerBean = (LoginBean) mCache.getAsObject("loginBean");
        ServiceLoginBean serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        String companyType = mCache.getAsString("companyType");
        if ("1".equals(companyType)) {
            if (ownerBean.getRows() != null) {
                String jsonBean = "{employeeId:\"" + ownerBean.getRows().getEmployeeId() + "\"}";
                ownerEmployeeHttp(jsonBean);
                Log.e("业主 个人信息jsonBean", jsonBean);
            }
        } else if ("2".equals(companyType)) {
            if (serviceBean.getRows() != null) {
                String jsonBean = "{employeeId:\"" + serviceBean.getRows().getEmployeeId() + "\"}";
                serviceEmployeeHttp(jsonBean);
                Log.e("服务商 个人信息jsonBean", jsonBean);
            }
        }
    }


    //业主个人信息
    private void ownerEmployeeHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.OWNER_EMPLOYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商个人信息
    private void serviceEmployeeHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.SERVICE_EMPLOYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_next://人员信息
                intent = new Intent(getActivity(), InformationActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_enterprise://企业信息
                intent = new Intent(getActivity(), EnterpriseInformationActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_search://查询
                intent = new Intent(getActivity(), WorkSearchActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_share://分享
                intent = new Intent(getActivity(), ShareActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_setting://设置
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主返回个人信息", result);
                InformationBean bean = new Gson().fromJson(result, InformationBean.class);
                if (bean.getMessage().getCode() == 200) {//加载成功

                    //头像路径
                    Glide.with(getActivity()).load(bean.getRows().getAvatar()).into(ivHead);
                    tvNickname.setText(bean.getRows().getName());
                    tvJobNumber.setText("工号 " + bean.getRows().getEmployeeCode());
                    tvWorkNumber.setText("已经解决工单 " + bean.getRows().getWorkOrderNumber());

                    tvNumber.setText("4.6"); //平均星级

                }
                break;
            case 2:
                Log.e("服务商返回个人信息", result);
                InformationBean serviceBean = new Gson().fromJson(result, InformationBean.class);
                if (serviceBean.getMessage().getCode() == 200) {

                    //头像路径
                    Glide.with(getActivity()).load(serviceBean.getRows().getAvatar()).into(ivHead);
                    tvNickname.setText(serviceBean.getRows().getName());
                    tvJobNumber.setText("工号 " + serviceBean.getRows().getEmployeeCode());
                    tvWorkNumber.setText("已经解决工单 " + serviceBean.getRows().getWorkOrderNumber());
                    tvNumber.setText("4.6"); //平均星级

                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
